/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package GUI;

import Clases.Consulta;
import java.util.LinkedList;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Ricardo
 */
public class ListarProveedoresEdicion extends javax.swing.JFrame {

    /**
     * Creates new form ListarProductos
     */
    LinkedList<String> codigosAEliminar = null;

    public ListarProveedoresEdicion() {
        initComponents();
    }

    public void SetCodigosEliminar(LinkedList<String> codigos) {
        codigosAEliminar = codigos;
    }

    public void llenarDatos() {
        if (Clases.Consulta.getProveedores().size() > 0) {
            DefaultTableModel modelo = (DefaultTableModel) tablaProveedores.getModel();
            String[] proveedor = new String[5];
            for (Clases.Proveedor p : Consulta.getProveedores()) {
                if (codigosAEliminar == null) {
                    proveedor[0] = p.getNit();
                    proveedor[1] = p.getNombre();
                    proveedor[2] = p.getCiudad();
                    proveedor[3] = p.getTelefono();
                    proveedor[4] = p.getDireccion();
                    modelo.addRow(proveedor);
                } else {
                    if (!codigosAEliminar.contains(p.getNit())) {
                        proveedor[0] = p.getNit();
                        proveedor[1] = p.getNombre();
                        proveedor[2] = p.getCiudad();
                        proveedor[3] = p.getTelefono();
                        proveedor[4] = p.getDireccion();
                        modelo.addRow(proveedor);
                    }
                }
            }
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        menuEdicion = new javax.swing.JPopupMenu();
        menuEditar = new javax.swing.JMenuItem();
        jSeparator1 = new javax.swing.JPopupMenu.Separator();
        menuEliminar = new javax.swing.JMenuItem();
        jPanel1 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tablaProveedores = new javax.swing.JTable();
        btnGuardar = new javax.swing.JButton();

        menuEditar.setText("Editar");
        menuEditar.setActionCommand("");
        menuEditar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                menuEditarActionPerformed(evt);
            }
        });
        menuEdicion.add(menuEditar);
        menuEdicion.add(jSeparator1);

        menuEliminar.setText("Eliminar");
        menuEliminar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                menuEliminarActionPerformed(evt);
            }
        });
        menuEdicion.add(menuEliminar);

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Edición de Proveedores");

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Proveedores"));

        tablaProveedores.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Nit", "Nombre del Proveedor", "Ciudad", "Teléfono", "Dirección"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false, false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        tablaProveedores.setComponentPopupMenu(menuEdicion);
        tablaProveedores.getTableHeader().setReorderingAllowed(false);
        jScrollPane1.setViewportView(tablaProveedores);
        tablaProveedores.getColumnModel().getColumn(0).setResizable(false);
        tablaProveedores.getColumnModel().getColumn(1).setResizable(false);
        tablaProveedores.getColumnModel().getColumn(2).setResizable(false);
        tablaProveedores.getColumnModel().getColumn(3).setResizable(false);
        tablaProveedores.getColumnModel().getColumn(4).setResizable(false);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 678, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 224, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        btnGuardar.setText("Guardar");
        btnGuardar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnGuardarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(btnGuardar, javax.swing.GroupLayout.PREFERRED_SIZE, 102, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(btnGuardar, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void menuEditarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_menuEditarActionPerformed
        // TODO add your handling code here:
        String proveedor = Consulta.getCodigoProveedor(tablaProveedores.getValueAt(tablaProveedores.getSelectedRow(), 1).toString());
        try {
            EditarProveedor ep = new EditarProveedor();
            ep.SetCodigosEliminarTemp(codigosAEliminar);
            ep.llenarDatos(proveedor);
            ep.setVisible(true);
            dispose();
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }//GEN-LAST:event_menuEditarActionPerformed

    private void menuEliminarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_menuEliminarActionPerformed
        // TODO add your handling code here:
        int confirm = JOptionPane.showConfirmDialog(null, "Esta Seguro que Desea Eliminar Este Registro?\n\nPodrá Deshacer Los Cambios Si Cierra el Formulario", "Eliminar Registro", 2);
        if (confirm == 0) {
            String codigo = tablaProveedores.getValueAt(tablaProveedores.getSelectedRow(), 0).toString();
            if (!codigo.equals("-1")) {
                if (codigosAEliminar == null) {
                    codigosAEliminar = new LinkedList<String>();
                }

                codigosAEliminar.add(codigo);
                try {
                    int fila = tablaProveedores.getSelectedRow();
                    DefaultTableModel modelo = (DefaultTableModel) tablaProveedores.getModel();
                    modelo.removeRow(fila);
                    tablaProveedores.setModel(modelo);
                } catch (Exception e) {
                    javax.swing.JOptionPane.showMessageDialog(null, "No ha seleccionado ninguna fila", "Seleccione una fila", 1);
                }
            }
        }
    }//GEN-LAST:event_menuEliminarActionPerformed

    private void btnGuardarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnGuardarActionPerformed
        // TODO add your handling code here:
        try {
            for (String codigo : codigosAEliminar) {
                //Consulta.eliminarProveedor(codigo);
            }
        } catch (Exception e) {
            javax.swing.JOptionPane.showMessageDialog(null, "Ha Ocurrido Un Error al Intentar Borrar los Registros", "Error de Sistema", 2);
        }
    }//GEN-LAST:event_btnGuardarActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnGuardar;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JPopupMenu.Separator jSeparator1;
    private javax.swing.JPopupMenu menuEdicion;
    private javax.swing.JMenuItem menuEditar;
    private javax.swing.JMenuItem menuEliminar;
    private javax.swing.JTable tablaProveedores;
    // End of variables declaration//GEN-END:variables
}
